package cn.com.self.apis.controller;


import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.com.self.apis.member.entity.UMemberInfo;
import cn.com.self.apis.member.service.IMemberInfoService;
import cn.com.self.apis.portal.entity.UDisputeEntity;
import cn.com.self.apis.portal.service.intf.IDisputeService;
import cn.com.self.apis.portal.util.ICustomPage;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.self.apis.escrow.entity.EscrowInfo;
import cn.com.self.apis.escrow.service.IEscrowInfoService;
import cn.com.selfsoft.smartcity.commons.http.DataItem;
import cn.com.selfsoft.smartcity.commons.http.DataPage;
import cn.com.selfsoft.smartcity.commons.http.ResultModel;
import cn.com.selfsoft.smartcity.commons.util.ParameterUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.com.self.apis.escrow.entity.EscrowMarginInfo;
import cn.com.self.apis.escrow.service.IEscrowMarginInfoService;
import cn.com.self.apis.util.CopyUtils;

/**
 *
 * @author1 ssh
 * @author2
 * @date 2021-04-28
 */
@Controller
@Api(tags = "托管中心")
public class CareCenterController {
    @Autowired
    private IEscrowInfoService escrowInfoService;

    @Autowired
    private IEscrowMarginInfoService escrowMarginInfoService;

    @Autowired
    private IMemberInfoService memberInfoService;

    /**新增托管
     * @Description //TODO
     * @return
     **/
    @RequestMapping(value = "/escrowInfo/create", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增托管信息")
    @Transactional
    public ResponseEntity create(@RequestBody EscrowInfo entity, HttpServletRequest request){
        ResponseEntity result = escrowInfoService.createEscrowInfo(entity);
        //entity.setCreateDate(new Date());
        //entity.setUpdateDate(new Date());
        //entity.setProgress("0%");
        //EscrowInfo create = escrowInfoService.create(entity);
        return result;
    }


    /**分页获取托管列表
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "分页获取托管列表")
    @RequestMapping(value = "/escrowInfo/page", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity getEscrowInfoPage(HttpServletRequest request, Pageable page){
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","sort","access_token");
        ICustomPage<EscrowInfo> pageData = this.escrowInfoService.page(queryParams, page);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(pageData));

    }

    /**获取托管信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "获取托管信息")
    @RequestMapping(value = {"/escrowInfo/load/{id}"}, method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity loadEscrowInfo(@PathVariable("id") String id,HttpServletRequest request) {
        EscrowInfo escrowInfo = this.escrowInfoService.loadEscrowInfoById(id);
        if(escrowInfo == null){
            return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(404,"该托管信息不存在！"));
        }
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(escrowInfo)));

    }


    /**修改托管信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "修改托管信息")
    @RequestMapping(value = {"/escrowInfo/update/{id}"}, method = RequestMethod.POST)
    @ResponseBody
    @Transactional
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody EscrowInfo entity, HttpServletRequest request) {
        return this.escrowInfoService.updateEscrowInfo(id,entity);

    }


    /**删除托管信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "删除托管信息")
    @ResponseBody
    @RequestMapping(value = "/escrowInfo/delete/{id}",method = RequestMethod.DELETE)
    @Transactional
    public ResponseEntity delete(@PathVariable("id") String id){
        this.escrowInfoService.delete(id);
        this.escrowInfoService.deleteUFile(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
    }


    /**新增托管保证金明细
     * @Description //TODO
     * @return
     **/
    @RequestMapping(value = "/escrowMarginInfo/create", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增托管信息新增托管保证金明细")
    public ResponseEntity create(@RequestBody EscrowMarginInfo entity, HttpServletRequest request){
        entity.setCreateDate(new Date());
        entity.setUpdateDate(new Date());
        EscrowMarginInfo create = escrowMarginInfoService.create(entity);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(create));
    }


    /**分页获取托管保证金明细列表
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "分页获取托管保证金明细列表")
    @RequestMapping(value = "/escrowMarginInfo/page", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity getEscrowMarginInfoPage(HttpServletRequest request, Pageable page){
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","sort","access_token");
        ICustomPage<EscrowMarginInfo> pageData = this.escrowMarginInfoService.page(queryParams, page);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(pageData));

    }

    /**获取托管保证金明细
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "获取托管保证金明细")
    @RequestMapping(value = {"/escrowMarginInfo/load/{id}"}, method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity loadEscrowMarginInfo(@PathVariable("id") String id,HttpServletRequest request) {
        EscrowMarginInfo escrowMarginInfo = this.escrowMarginInfoService.load(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(escrowMarginInfo)));

    }


    /**修改托管保证金明细信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "修改托管保证金明细信息")
    @RequestMapping(value = {"/escrowMarginInfo/update/{id}"}, method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody EscrowMarginInfo entity, HttpServletRequest request) {
        EscrowMarginInfo escrowMarginInfo = this.escrowMarginInfoService.load(id);
        CopyUtils.copyProperties(entity, escrowMarginInfo);
        escrowMarginInfo.setUpdateDate(new Date());
        EscrowMarginInfo update = this.escrowMarginInfoService.update(escrowMarginInfo);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(update)));

    }


    /**删除托管保证金明细信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "删除托管保证金明细信息")
    @ResponseBody
    @RequestMapping(value = "/escrowMarginInfo/delete/{id}",method = RequestMethod.DELETE)
    public ResponseEntity deletes(@PathVariable("id") String id){
        this.escrowMarginInfoService.delete(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
    }

    @Autowired
    private IDisputeService disputeService;
    /**新增纠纷申请
     * @Description //TODO
     * @return
     **/
    @RequestMapping(value = "/dispute/create", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增纠纷申请")
    @Transactional
    public ResponseEntity create(@RequestBody UDisputeEntity entity, HttpServletRequest request){
        UMemberInfo uMemberInfo = memberInfoService.load(entity.getMemberId().toString());
        if(uMemberInfo == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ResultModel<>(404, "该发起人不存在！"));
        }
        UDisputeEntity create = disputeService.createDispute(entity);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(create));
    }


    /**分页获取纠纷申请列表
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "分页获取纠纷申请列表")
    @RequestMapping(value = "/dispute/page", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity getDisputePage(HttpServletRequest request, Pageable page){
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","sort","access_token");
        ICustomPage<UDisputeEntity> pageData = this.disputeService.page(queryParams, page);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(pageData));

    }

    /**获取纠纷申请信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "获取纠纷申请信息")
    @RequestMapping(value = {"/dispute/load/{id}"}, method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity loadDisputeInfo(@PathVariable("id") String id, HttpServletRequest request) {
        UDisputeEntity disputeEntity = this.disputeService.loadDisputeInfoById(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(disputeEntity)));

    }


    /**修改纠纷申请信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "修改纠纷申请信息")
    @RequestMapping(value = {"/dispute/update/{id}"}, method = RequestMethod.POST)
    @ResponseBody
    @Transactional
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody UDisputeEntity entity, HttpServletRequest request) {
        UDisputeEntity update = this.disputeService.updateDispute(id,entity);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(update)));

    }


    /**删除纠纷申请信息
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "删除纠纷申请信息")
    @ResponseBody
    @RequestMapping(value = "/dispute/delete/{id}",method = RequestMethod.DELETE)
    @Transactional
    public ResponseEntity deleteDispute(@PathVariable("id") String id){
        this.disputeService.delete(id);
        this.disputeService.deleteUFile(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
    }
}

